package com.atguigu.gmall.ums.controller;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gmall.ums.entity.UserEntity;
import com.atguigu.gmall.ums.service.UserService;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.bean.PageParamVo;

/**
 * 用户表
 *
 * @author rhine
 * @email gangge@atguigu.com
 * @date 2022-04-21 10:57:00
 */
@Api(tags = "用户表 管理")
@RestController
@RequestMapping("ums/user/")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 注册接口：
     */
    @PostMapping("register")
    public ResponseVo<Boolean> register(@RequestBody UserEntity userEntity, String code) {
        Boolean flag = userService.register(userEntity, code);
        return ResponseVo.ok(flag);
    }


    /**
     * 使用账号和密码查询账户接口：
     *
     * 查询到了将userEntity传给前端，查不到，传null
     */
    @PostMapping("queryUser")//用post请求的原因是因为，以后sso要用
    public ResponseVo<UserEntity> checkAccount(@RequestParam("loginName") String account, @RequestParam("password") String password) {
        UserEntity userEntity = userService.checkAccount(account, password);
        return ResponseVo.ok(userEntity);
    }


    /**
     * 检查用户参数是否被占用的接口
     * 检查username、phone、email三个参数不重复就可以了
     * check/18232323232/2  --2表示检查自的类型是phone，上一级路径表示检查的手机号码值
     * 1- username
     * 2- phone
     * 3- email
     */
    @GetMapping("check/{data}/{type}")
    public ResponseVo<Boolean> check(@PathVariable("data") String data, @PathVariable("type") Integer type) {
        Boolean flag = userService.check(data, type);
        return ResponseVo.ok(flag);
    }


    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> queryUserByPage(PageParamVo paramVo) {
        PageResultVo pageResultVo = userService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<UserEntity> queryUserById(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);

        return ResponseVo.ok(user);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody UserEntity user) {
        userService.save(user);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody UserEntity user) {
        userService.updateById(user);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids) {
        userService.removeByIds(ids);
        return ResponseVo.ok();
    }

}
